//
//  NYArticleDetailViewController.m
//  Nooryes
//
//  Created by baken on 2020/3/5.
//  Copyright © 2020 MyApp. All rights reserved.
//

#import "NYArticleDetailViewController.h"
#import "CHTCollectionViewWaterfallLayout.h"
#import "NYRecommendCollectionViewCell.h"
#import "NYRecommendViewModel.h"
#import "NYArticleListModel.h"
#import "NYArticleDetailModel.h"
#import "SDCycleScrollView.h"
#import "WebViewController.h"
#import "NYOtherUserViewController.h"
#import "NYTopicArticlesViewController.h"
#import "NYAlertView.h"
#import "AccountModel.h"
#import "NYCommentEditView.h"
#import "NYCommentListModel.h"
#import "NYPostMulReplyTableViewCell.h"
#import "NYLoginViewController.h"
#import "NYMoreReplyViewController.h"
#import <ShareSDK/ShareSDK.h>
#import <ShareSDKUI/ShareSDK+SSUI.h>
#import "TopicCategoryModel.h"

@interface NYArticleDetailViewController ()<SDCycleScrollViewDelegate,CommentEditDelegate>

@property (weak, nonatomic) IBOutlet UIView *sliderView;
@property (weak, nonatomic) IBOutlet UILabel *mytitle;
@property (weak, nonatomic) IBOutlet UILabel *name;
@property (weak, nonatomic) IBOutlet UILabel *date;
@property (weak, nonatomic) IBOutlet UIImageView *avatar;
@property (weak, nonatomic) IBOutlet UILabel *liuyan;
@property (weak, nonatomic) IBOutlet UILabel *dianzan;
@property (weak, nonatomic) IBOutlet UILabel *collect;
@property (weak, nonatomic) IBOutlet UILabel *label2;
@property (weak, nonatomic) IBOutlet UIView *topicView;

@property (weak, nonatomic) IBOutlet UIImageView *goodsImg;
@property (weak, nonatomic) IBOutlet UILabel *goodsTitle;
@property (weak, nonatomic) IBOutlet UIButton *typeBtn;

@property (weak, nonatomic) IBOutlet UIButton *collectBtn;

@property (nonatomic, strong) NYMoreReplyViewController *moreReplyVC;

@property (nonatomic, strong) NYRecommendViewModel *recommendVM;


@property (weak, nonatomic) IBOutlet UITextView *textView;

@property (nonatomic, strong) SDCycleScrollView *adScrollView;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *slideHeight;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *classW;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *topicHeight;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *goodsHeight;

@property (weak, nonatomic) IBOutlet UITableView *commentTableView;

@property (nonatomic, strong) NYCommentEditView *commentEditView;

@property (weak, nonatomic) IBOutlet UILabel *commentCountLabel;

@property (weak, nonatomic) IBOutlet NSLayoutConstraint *commentTableViewH;

@property (weak, nonatomic) IBOutlet UIScrollView *scrollView;

@property (weak, nonatomic) IBOutlet UIView *commentView;

@property (weak, nonatomic) IBOutlet UIView *goodsView;

@property (nonatomic, strong) UIView *editOverView;

@end

@implementation NYArticleDetailViewController

-(void)viewDidLayoutSubviews{
    [super viewDidLayoutSubviews];
    if (self.recommendVM.articleDetail) {
        CGFloat height = kScreenWidth * self.recommendVM.articleDetail.hdheight /self.recommendVM.articleDetail.hdwidth;
        self.adScrollView.frame = CGRectMake(0, 0, kScreenWidth, height);
        [self.slideHeight setConstant:height];
    }
    
}

- (void)viewDidLoad {
    [super viewDidLoad];
    
    
    self.recommendVM.articleID = self.articleID;
    [self.recommendVM getArticleDetailWithCompletionHandle:^(NSError *error) {
        if (!error) {
            [self.sliderView addSubview:self.adScrollView];
            self.textView.text = self.recommendVM.articleDetail.content;
            [self.textView sizeToFit];
            [self.avatar sd_setImageWithURL:[NSURL URLWithString:self.recommendVM.articleDetail.userimg]];
            self.mytitle.text = self.recommendVM.articleDetail.title;
            [self.mytitle sizeToFit];
            self.date.text = self.recommendVM.articleDetail.pubtime;
            self.collect.text = self.recommendVM.articleDetail.collect;
            self.dianzan.text = self.recommendVM.articleDetail.zan;
            self.liuyan.text = self.recommendVM.articleDetail.look;
            self.name.text = self.recommendVM.articleDetail.nickname;
            self.label2.text = self.recommendVM.articleDetail.classname;
            [self.classW setConstant:self.recommendVM.articleDetail.classWidth];
            [self addTopicButton];
            if (!IsStrEmpty(self.recommendVM.articleDetail.shoptitle)) {
                [self.goodsImg sd_setImageWithURL:[NSURL URLWithString:self.recommendVM.articleDetail.shopimg]];
                self.goodsTitle.text = self.recommendVM.articleDetail.shoptitle;
                self.goodsView.hidden = NO;
            } else {
                [self.goodsHeight setConstant:-25];
            }
            
            if ([self.recommendVM.articleDetail.type isEqualToString:@"1"]) {
                if (self.recommendVM.articleDetail.iszan) {
                    [self.typeBtn setImage:[UIImage imageNamed:@"btn_zhongcao_r"] forState:UIControlStateNormal];
                } else {
                    [self.typeBtn setImage:[UIImage imageNamed:@"btn_zhongcao"] forState:UIControlStateNormal];
                }
                
                
            } else {
                if (self.recommendVM.articleDetail.iszan) {
                    [self.typeBtn setImage:[UIImage imageNamed:@"btn_bacao_r"] forState:UIControlStateNormal];
                } else {
                    [self.typeBtn setImage:[UIImage imageNamed:@"btn_bacao"] forState:UIControlStateNormal];
                }
            }
            if (self.recommendVM.articleDetail.iscollect) {
                [self.collectBtn setImage:[UIImage imageNamed:@"btn_collection_r"] forState:UIControlStateNormal];
            }
        }
        else {
            [self.navigationController popViewControllerAnimated:YES];
        }
    }];
    
    
    [self.recommendVM refreshCommentsCompletionHandle:^(NSError *error) {
        if (!error) {
            self.commentCountLabel.text = [NSString stringWithFormat:@"共%lu条评论",(unsigned long)self.recommendVM.commentsArray.count];
            [self.commentTableViewH setConstant:self.recommendVM.commentHeight];
            [self.commentTableView reloadData];
        }
    }];
    
    self.scrollView.mj_footer = [MJRefreshBackNormalFooter footerWithRefreshingBlock:^{
        [self.recommendVM getMoreCommentsCompletionHandle:^(NSError *error) {
            self.commentCountLabel.text = [NSString stringWithFormat:@"共%lu条评论",(unsigned long)self.recommendVM.commentsArray.count];
            [self.commentTableViewH setConstant:self.recommendVM.commentHeight];
            [self.commentTableView reloadData];
        }];
    }];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillShow:) name:UIKeyboardWillShowNotification object:nil];
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(keyboardWillHide:) name:UIKeyboardWillHideNotification object:nil];
    
    [[NSNotificationCenter defaultCenter] addObserver:self selector:@selector(didLogin) name:@"login" object:nil];
    
}

-(void)addTopicButton{
    CGFloat w = 0;
    CGFloat h = 0;
    int tag = 0;
    
    CGFloat viewWidth = kScreenWidth - 24;
    UIView *buttonView1 = [[UIView alloc] init];
    UIView *buttonView2 = [[UIView alloc] init];
    UIView *buttonView3 = [[UIView alloc] init];
    
    for (int i = 0; i < self.recommendVM.articleDetail.talks.count; i++) {
        TopicCategoryModel *model = self.recommendVM.articleDetail.talks[i];
        UIButton *button = [UIButton buttonWithType:UIButtonTypeSystem];
        button.tag = [model.talkid intValue];
        button.backgroundColor = UIColorFromRGB(0xe9edf4);
        button.layer.cornerRadius = 12;
        [button addTarget:self action:@selector(onTopic:) forControlEvents:UIControlEventTouchUpInside];
        [button setTitleColor:UIColorFromRGB(0x256ee9) forState:UIControlStateNormal];
        button.titleLabel.font = [UIFont systemFontOfSize:13];
        [button setTitle:model.name forState:UIControlStateNormal];
        
        //当button的位置超出屏幕边缘时换行  只是button所在父视图的宽度
        if(12 + w + model.width  > viewWidth){
            w = 0; //换行时将w置为0
            h = h + 28;//距离父视图也变化
            tag ++;
            button.frame = CGRectMake(12 + w, 0, model.width, 24);//重设button的frame
        }else{
            button.frame = CGRectMake(12 + w, 0, model.width ,24);
        }
        w = button.frame.size.width + button.frame.origin.x;
        
        switch (tag) {
            case 0:
                [buttonView1 addSubview:button];
                buttonView1.frame = CGRectMake(0 , h, w + model.width, 24);
                break;
            case 1:
                [buttonView2 addSubview:button];
                buttonView2.frame = CGRectMake(0 , h, w + model.width, 24);
                break;
            case 2:
                [buttonView3 addSubview:button];
                buttonView3.frame = CGRectMake(0 , h, w + model.width, 24);
            default:
                break;
        }
    }
    [self.topicView addSubview:buttonView1];
    [self.topicView addSubview:buttonView2];
    [self.topicView addSubview:buttonView3];
    [self.topicHeight setConstant:(tag+1)*28];
}

//-(void)viewDidLayoutSubviews{

//
//    if (self.recommendVM.articleDetail) {
//        [[[UIImageView alloc] init] sd_setImageWithURL:[NSURL URLWithString:self.recommendVM.articleDetail.images[0]] completed:^(UIImage * _Nullable image, NSError * _Nullable error, SDImageCacheType cacheType, NSURL * _Nullable imageURL) {
//              if (image) {
//                  CGFloat hh = image.size.height;
//                  CGFloat ww = image.size.width;
//                  CGFloat height = kScreenWidth * image.size.height /image.size.width;
//                  [self.slideHeight setConstant:height];
//                  self.adScrollView.frame = CGRectMake(0, 0, kScreenWidth, height);
//              }
//          }];
//    }
//}


-(void)didLogin{
    [self.recommendVM getArticleDetailWithCompletionHandle:^(NSError *error) {
        if (!error) {
            if ([self.recommendVM.articleDetail.type isEqualToString:@"1"]) {
                if (self.recommendVM.articleDetail.iszan) {
                    [self.typeBtn setImage:[UIImage imageNamed:@"btn_bacao_r"] forState:UIControlStateNormal];
                }
                
            } else {
                if (self.recommendVM.articleDetail.iszan) {
                    [self.typeBtn setImage:[UIImage imageNamed:@"btn_zhongcao_r"] forState:UIControlStateNormal];
                }
            }
            if (self.recommendVM.articleDetail.iscollect) {
                [self.collectBtn setImage:[UIImage imageNamed:@"btn_collection_r"] forState:UIControlStateNormal];
            }
            
        }
    }];
    
}

-(void)dealloc{
    [[NSNotificationCenter defaultCenter] removeObserver:self];
}



-(SDCycleScrollView *)adScrollView{
    if (!_adScrollView) {
        _adScrollView = [SDCycleScrollView cycleScrollViewWithFrame:self.sliderView.bounds delegate:self placeholderImage:nil];
        _adScrollView.scrollDirection = UICollectionViewScrollDirectionHorizontal;
        _adScrollView.pageControlAliment = SDCycleScrollViewPageContolAlimentRight;
        _adScrollView.autoScrollTimeInterval = 3.0f;
        _adScrollView.imageURLStringsGroup = [self.recommendVM.articleDetail.images copy];
    }
    
    
    return _adScrollView;
}

//
//
//#pragma mark - UICollectionView Delegate
//
//
//- (NSInteger)collectionView:(UICollectionView *)collectionView numberOfItemsInSection:(NSInteger)section {
//    return self.recommendVM.dataArr.count;
//}
//
//
//- (UICollectionViewCell *)collectionView:(UICollectionView *)collectionView cellForItemAtIndexPath:(NSIndexPath *)indexPath {
//    NYRecommendCollectionViewCell *cell = [NYRecommendCollectionViewCell cellWithCollectionView:collectionView indePath:indexPath];
//    ArticleListModel *model = [self.recommendVM.dataArr objectAtIndex:indexPath.row];
//    cell.model = model;
//    [cell.imageView sd_setImageWithURL:[NSURL URLWithString:model.image] placeholderImage:[UIImage imageNamed:@"loading"] completed:^(UIImage *image, NSError *error, SDImageCacheType cacheType, NSURL *imageURL) {
//        if (image) {
//            if (!CGSizeEqualToSize(model.imageSize, image.size)) {
//                model.imageSize = image.size;
//                [collectionView reloadItemsAtIndexPaths:@[indexPath]];
//            }
//        }
//    }];
//
//    return cell;
//}
//
//
//-(void)collectionView:(UICollectionView *)collectionView didSelectItemAtIndexPath:(NSIndexPath *)indexPath{
//
//}
//
//
//#pragma mark - CHTCollectionViewDelegateWaterfallLayout
//- (CGSize)collectionView:(UICollectionView *)collectionView layout:(UICollectionViewLayout *)collectionViewLayout sizeForItemAtIndexPath:(NSIndexPath *)indexPath {
//    ArticleListModel *model = [self.recommendVM.dataArr objectAtIndex:indexPath.row];
//    if (!CGSizeEqualToSize(model.imageSize, CGSizeZero)) {
//        return model.imageSize;
//    }
//    return CGSizeMake(150, 150);
//}


-(NYRecommendViewModel *)recommendVM{
    if (!_recommendVM) {
        _recommendVM = [NYRecommendViewModel new];
    }
    return _recommendVM;
}



- (IBAction)onBuy:(id)sender {
    //
    //    WebViewController *webVC = [[WebViewController alloc] init];
    //    webVC.urlString = self.recommendVM.articleDetail.shoplink;
    //    [self.navigationController pushViewController:webVC animated:YES];
    [[UIApplication sharedApplication] openURL:[NSURL URLWithString:self.recommendVM.articleDetail.shoplink] options:[NSDictionary dictionary] completionHandler:nil];
}

- (void)onTopic:(id)sender {
    UIButton *btn = (UIButton *)sender;
    UIStoryboard *board = [UIStoryboard storyboardWithName:@"TopicStoryboard" bundle:nil];
    NYTopicArticlesViewController *listVC = [board instantiateViewControllerWithIdentifier:@"NYTopicArticlesViewController"];
    listVC.topicID = [NSString stringWithFormat:@"%li",(long)btn.tag];
    [self.navigationController pushViewController:listVC animated:YES];
}

- (IBAction)onRemove:(id)sender {
    if ([self isLogin]) {
        [self.recommendVM likeArticleWithCompletionHandle:^(NSError *error) {
            if (!error) {
                if ([self.recommendVM.articleDetail.type isEqualToString:@"1"]) {
                    if (self.recommendVM.articleDetail.iszan) {
                        myDelegate.account.zan++;
                        [self.typeBtn setImage:[UIImage imageNamed:@"btn_zhongcao_r"] forState:UIControlStateNormal];
                    } else {
                        myDelegate.account.zan--;
                        [self.typeBtn setImage:[UIImage imageNamed:@"btn_zhongcao"] forState:UIControlStateNormal];
                    }
                } else {
                    if (self.recommendVM.articleDetail.iszan) {
                        myDelegate.account.zan++;
                        [self.typeBtn setImage:[UIImage imageNamed:@"btn_bacao_r"] forState:UIControlStateNormal];
                    } else {
                        myDelegate.account.zan--;
                        [self.typeBtn setImage:[UIImage imageNamed:@"btn_bacao"] forState:UIControlStateNormal];
                    }
                }
            } else {
                NYAlertView *alert = [NYAlertView sharedAlertView];
                alert.title = [error.userInfo objectForKey:@"NSLocalizedDescription"];
                [alert showAlert:^{
                }];
            }
        }];
    }
}



- (IBAction)onCollect:(id)sender {
    if ([self isLogin]) {
        self.recommendVM.type = @"1";
        self.recommendVM.dataID = self.articleID;
        [self.recommendVM collectArticleWithCompletionHandle:^(NSError *error) {
            if (!error) {
                if (self.recommendVM.articleDetail.iscollect) {
                    myDelegate.account.collect++;
                    [self.collectBtn setImage:[UIImage imageNamed:@"btn_collection_r"] forState:UIControlStateNormal];
                } else {
                    myDelegate.account.collect--;
                    [self.collectBtn setImage:[UIImage imageNamed:@"btn_collection"] forState:UIControlStateNormal];
                }
                
            } else {
                NYAlertView *alert = [NYAlertView sharedAlertView];
                alert.title = [error.userInfo objectForKey:@"NSLocalizedDescription"];
                [alert showAlert:^{}];
            }
        }];
    }
}

- (IBAction)onShare:(id)sender {
    NSString *content;
    if (self.recommendVM.articleDetail.content.length > 10) {
        content = [self.recommendVM.articleDetail.content substringToIndex:10];
    } else {
        content = self.recommendVM.articleDetail.content;
    }
    NSMutableDictionary *shareParams = [NSMutableDictionary dictionary];
    [shareParams SSDKSetupShareParamsByText:content
                                     images:@[self.recommendVM.articleDetail.image]
                                        url:[NSURL URLWithString:self.recommendVM.articleDetail.shareurl]
                                      title:self.recommendVM.articleDetail.title
                                       type:SSDKContentTypeAuto];
    
    [ShareSDK showShareActionSheet:nil
                       customItems:nil
                       shareParams:shareParams
                sheetConfiguration:nil
                    onStateChanged:^(SSDKResponseState state, SSDKPlatformType platformType, NSDictionary *userData, SSDKContentEntity *contentEntity, NSError *error, BOOL end) {
        switch (state) {
            case SSDKResponseStateSuccess:
            {
                if (platformType != SSDKPlatformTypeWechat) {
                    NYAlertView *alert = [NYAlertView sharedAlertView];
                    alert.title = @"分享成功";
                    [alert showAlert:^{ }];
                    break;
                }
                
            }
            case SSDKResponseStateFail:
            {
                NYAlertView *alert = [NYAlertView sharedAlertView];
                alert.title = @"分享失败";
                [alert showAlert:^{ }];
                break;
            }
            default:
                break;
        }
    }];
}


- (IBAction)onUser:(id)sender {
    UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"TopicStoryboard" bundle:nil];
    NYOtherUserViewController *detailVC =[storyboard  instantiateViewControllerWithIdentifier:@"NYOtherUserViewController"];
    detailVC.userID = self.recommendVM.articleDetail.userid;
    [self.navigationController pushViewController:detailVC animated:YES];
    
}


#pragma mark -
#pragma mark 评论

//点击主页底部的评论
- (IBAction)onCommentList:(id)sender {
    if (self.scrollView.contentSize.height > self.scrollView.frame.size.height) {
        CGFloat height = self.scrollView.contentSize.height - self.scrollView.frame.size.height;
        CGPoint point;
        if (height > self.scrollView.frame.size.height)  {
            point = CGPointMake(0, self.commentView.frame.origin.y);
        } else {
            point = CGPointMake(0, height);
        }
        [self.scrollView setContentOffset:point animated:YES];
    }
}

//弹出评论框
- (void)onComment:(NSString *)parID {
    self.recommendVM.commentid = parID;
    if ([self isLogin]) {
        self.editOverView = [[UIView alloc] initWithFrame:[[UIScreen mainScreen] bounds]];
        self.editOverView.backgroundColor = [UIColor colorWithWhite:0.1 alpha:0.8];
        UITapGestureRecognizer *tapper = [[UITapGestureRecognizer alloc] initWithTarget:self action:@selector(editOverDismiss)];
        [self.editOverView addGestureRecognizer:tapper];
        [self.editOverView addSubview:self.commentEditView];
        [self.commentEditView.textView becomeFirstResponder];
        [myDelegate.window addSubview:self.editOverView];
    }
}

//发送评论
-(void)sendComment{
    NSString *str = [self.commentEditView.textView.text stringByTrimmingCharactersInSet:[NSCharacterSet whitespaceAndNewlineCharacterSet]];
    if (str.length>0) {
        self.recommendVM.content = self.commentEditView.textView.text;
        self.recommendVM.title = self.recommendVM.articleDetail.title;
        if ([self.recommendVM.commentid isEqualToString:@"0"]) {
            [self.recommendVM postCommentWithCompletionHandle:^(NSError *error) {
                NYAlertView *alert = [NYAlertView sharedAlertView];
                if (!error) {
                    alert.title = @"评论成功";
                    [alert showAlert:^{
                        [self didCommentNews];
                    }];
                    
                } else {
                    alert.title = [error.userInfo objectForKey:@"NSLocalizedDescription"];
                    [alert showAlert:^{
                    }];
                }
                [self editOverDismiss];
            }];
        } else {
            [self.recommendVM replyCommentWithCompletionHandle:^(NSError *error) {
                NYAlertView *alert = [NYAlertView sharedAlertView];
                if (!error) {
                    alert.title = @"评论成功";
                    [alert showAlert:^{
                        [self didCommentNews];
                    }];
                    
                } else {
                    alert.title = [error.userInfo objectForKey:@"NSLocalizedDescription"];
                    [alert showAlert:^{
                    }];
                }
                [self editOverDismiss];
            }];
        }
    } else {
        NYAlertView *alert = [NYAlertView sharedAlertView];
        alert.title = @"请输入评论内容";
        [alert showAlert:^{
        }];
    }
}

- (void)didCommentNews {
    [self.recommendVM refreshCommentsCompletionHandle:^(NSError *error) {
        if (!error) {
            self.commentCountLabel.text = [NSString stringWithFormat:@"共%lu条评论",(unsigned long)self.recommendVM.commentsArray.count];
            [self.commentTableViewH setConstant:self.recommendVM.commentHeight];
            [self.commentTableView reloadData];
        }
    }];
}


-(void)cancelComment{
    [self editOverDismiss];
}

- (void)editOverDismiss{
    self.commentEditView.textView.text = @"";
    [self.commentEditView.textView resignFirstResponder];
    [self.editOverView removeFromSuperview];
    self.editOverView = nil;
    
}


- (NYCommentEditView *)commentEditView{
    if (!_commentEditView) {
        _commentEditView = [[NYCommentEditView alloc] initWithFrame:CGRectMake(0, kScreenHeight, kScreenWidth, 140)];
        _commentEditView.delegate = self;
    }
    return _commentEditView;
}

- (BOOL)textFieldShouldBeginEditing:(UITextField *)textField{
    [textField resignFirstResponder];
    [self onComment:@"0"];
    return NO;
}

#pragma mark <UITableViewDataSource>

- (NSInteger)tableView:( UITableView *)tableView numberOfRowsInSection:(NSInteger)section {
    return self.recommendVM.commentsArray.count == 0 ? 1 : self.recommendVM.commentsArray.count;
}


- (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.recommendVM.commentsArray.count == 0) {
        UITableViewCell *cell = [[UITableViewCell alloc] initWithStyle:UITableViewCellStyleDefault reuseIdentifier:@"normalCell"];
        UIImageView *image = [[UIImageView alloc] initWithFrame:CGRectMake((kScreenWidth-150) * 0.5, 12, 140, 120)];
        image.image = [UIImage imageNamed:@"wup"];
        image.contentMode = UIViewContentModeScaleAspectFill;
        cell.selectionStyle = UITableViewCellSelectionStyleNone;
        cell.backgroundColor = [UIColor clearColor];
        [cell addSubview:image];
        return cell;
    } else{
        CommentListSubModel *model = [self.recommendVM.commentsArray objectAtIndex:indexPath.row];
        NYPostMulReplyTableViewCell *cell = [NYPostMulReplyTableViewCell cellWithTableView:tableView];
        cell.model = model;
        cell.indexPath = indexPath;
        __weak NYArticleDetailViewController *weakSelf = self;
        cell.replyingBlock = ^(NSString *parentID){ //回复评论
            [weakSelf onComment:parentID];
        };
        cell.loadMoreBlock = ^(NSIndexPath *index){
            [weakSelf onReplyMore:index];
        };
        cell.avatarBlock = ^(NSString *uid) {
            [weakSelf onCommentUser:uid];
            
        };
        
        return cell;
    }
    
}

-(void)onCommentUser:(NSString *)uid{
    if (!IsStrEmpty(uid)) {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"TopicStoryboard" bundle:nil];
        NYOtherUserViewController *detailVC =[storyboard  instantiateViewControllerWithIdentifier:@"NYOtherUserViewController"];
        detailVC.userID = uid;
        [self.navigationController pushViewController:detailVC animated:YES];
    }
}

//点击查看更多回复
-(void)onReplyMore:(NSIndexPath *)index{
    if ([self isLogin]) {
        CommentListSubModel *model = [self.recommendVM.commentsArray objectAtIndex:index.row];
        self.moreReplyVC.model = model;
        __weak NYArticleDetailViewController *weakSelf = self;
        self.moreReplyVC.commentBlock = ^(NSString *parentID){
            [weakSelf onComment:parentID];
        };
        [self presentViewController:self.moreReplyVC animated:YES completion:nil];
    }
}

-(CGFloat)tableView:(UITableView *)tableView heightForRowAtIndexPath:(NSIndexPath *)indexPath{
    if (self.recommendVM.commentsArray.count == 0) {
        return 150;
    }
    CommentListSubModel *model = [self.recommendVM.commentsArray objectAtIndex:indexPath.row];
    return model.cellHeight;
    
}

#pragma mark 键盘通知

//当键盘出现或改变时调用
- (void)keyboardWillShow:(NSNotification *)notification {
    NSDictionary *info = [notification userInfo];
    CGFloat endHeight = [[info objectForKey:UIKeyboardFrameEndUserInfoKey] CGRectValue].size.height;
    [UIView animateWithDuration:0.1 animations:^{
        self.commentEditView.frame = CGRectMake(0, kScreenHeight - endHeight - 140, kScreenWidth, 140);
    }];
    //    [self.view bringSubviewToFront:self.commentEditView];
}

// 当键退出时调用
- (void)keyboardWillHide:(NSNotification *)notification {
    [UIView animateWithDuration:0.1 animations:^{
        self.commentEditView.frame = CGRectMake(0, kScreenHeight, kScreenWidth, 140);
    }];
}




- (BOOL)isLogin{
    if (myDelegate.account) {
        return YES;
    } else {
        //        UIAlertController *alertVC = [UIAlertController alertControllerWithTitle:@"请先登录后评论。" message:nil preferredStyle:UIAlertControllerStyleAlert];
        //        [alertVC addAction:[UIAlertAction actionWithTitle:@"取消" style:UIAlertActionStyleCancel handler:nil]];
        //        [alertVC addAction:[UIAlertAction actionWithTitle:@"登录" style:UIAlertActionStyleDefault handler:^(UIAlertAction * _Nonnull action) {
        UIStoryboard *storyboard = [UIStoryboard storyboardWithName:@"NYLoginStoryboard" bundle:nil];
        NYLoginViewController *vc = [storyboard instantiateViewControllerWithIdentifier:@"NYLoginViewController"];
        vc.controller = self;
        [self.navigationController pushViewController:vc animated:YES];
        //        }]];
        //        [self presentViewController:alertVC animated:YES completion:nil];
        return NO;
    }
}



-(NYMoreReplyViewController *)moreReplyVC{
    if (!_moreReplyVC) {
        _moreReplyVC = [[NYMoreReplyViewController alloc] initWithNibName:@"NYMoreReplyViewController" bundle:nil];
        _moreReplyVC.modalPresentationStyle = UIModalPresentationOverFullScreen;
        _moreReplyVC.modalTransitionStyle = UIModalTransitionStyleCrossDissolve;
    }
    return _moreReplyVC;
}

@end
